from django.conf import settings
from django.db import models
from django.urls import reverse
from django.utils import timezone

from account.models import User
from common.models import Common, CommonSocial, MusicInfo
from shop.models import ShopInfo


class KillProduct(Common, CommonSocial):
    """秒杀"""
    shop = models.ForeignKey(ShopInfo, verbose_name=u'店铺', null=True, on_delete=models.SET_NULL, related_name='kp_shop')
    title = models.CharField(u'商品名称', max_length=255, default='')
    image = models.CharField(u'图片', max_length=1024, default='')
    start_time = models.DateTimeField(u'开始时间')
    end_time = models.DateTimeField(u'结束时间')
    price_original = models.IntegerField(u'原价(分)', default=0)
    price_discount = models.IntegerField(u'现价(分)', default=0)
    desc = models.TextField(u'介绍', default='')
    content = models.TextField(u'内容', default='', blank=True)
    inventory = models.IntegerField(u'库存', default=-1, blank=True, help_text=u'库存数量，-1为库存无限')
    shelve = models.BooleanField(u'上架', default=False, help_text=u'False为下架，True为上架')
    priority = models.IntegerField(u'优先级', default=0, blank=True)
    contact_phone = models.CharField(u'联系电话', max_length=100, default='', blank=True)
    music = models.ForeignKey(MusicInfo, verbose_name=u'背景音乐', on_delete=models.SET_NULL, null=True, blank=True)

    def __unicode__(self):
        return '%s-%s' % (self.id, self.title)

    def get_image_url(self):
        return settings.QINIU_DOMAIN + self.image

    def has_start(self):
        return self.start_time < timezone.now()

    def has_end(self):
        return self.end_time < timezone.now()

    def get_music_url(self):
        if self.music:
            return self.music.get_path_url()
        return ''

    def get_inventory_status(self):
        if self.inventory == -1:
            return True
        return self.inventory > self.sale

    def get_mobile_url(self):
        return 'http://qxb.dongpouu.com%s?shop=%s' % (reverse('kill:detail', args=[self.id]), self.shop_id)

    def get_buy_url(self):
        return '%s?shop=%s' % (reverse('order:kill_create_api'), self.shop_id)

    def get_pay_url(self):
        return '%s?shop=%s' % (reverse('order:pay_kill'), self.shop_id)

    class Meta:
        ordering = ['-create_time']
        verbose_name = '秒杀活动'
        db_table = 'kill'


class KillRecord(Common):
    """秒杀记录"""
    shop = models.ForeignKey(ShopInfo, verbose_name=u'店铺', on_delete=models.CASCADE, related_name='kr_shop', null=True,
                             blank=True)
    user = models.ForeignKey(User, verbose_name=u'积分账户', on_delete=models.SET_NULL, related_name='kr_user', null=True,
                             blank=True)
    name = models.CharField(u'姓名', max_length=25, default='')
    phone = models.CharField(u'手机号', max_length=25, default='')
    info = models.ForeignKey(KillProduct, verbose_name=u'购买商品', null=True, on_delete=models.SET_NULL,
                             related_name='kr_product', blank=True)
    order = models.ForeignKey('order.Order', verbose_name=u'订单记录', null=True, blank=True, on_delete=models.SET_NULL)
    refund = models.ForeignKey('order.RefundOrder', verbose_name=u'退单记录', null=True, blank=True,
                               on_delete=models.SET_NULL)
    cash = models.BooleanField(u'兑换', default=False)
    cash_time = models.DateTimeField(u'兑换时间', null=True, blank=True)
    comment = models.TextField(u'备注', default='', blank=True)

    class Meta:
        db_table = 'kill_record'
        verbose_name = '秒杀记录'
        ordering = ['-create_time']
